// function qs(ele) {
//     return document.querySelector(ele)
// }

// 接口地址：https://www.apifox.cn/apidoc/shared-36f0cbc0-9064-44bc-850a-805e263bf494/api-25827426
const qs = (ele) => document.querySelector(ele)
axios.defaults.baseURL = "http://124.223.14.236:3001/api"




// 封装省
async function getProvinceData() {

    const { data: res } = await axios.get('/city/province')
    const str = res.data.map((item) =>
        `				<option value="${item.province}">${item.name}</option>`
    ).join('')
    qs('#province').innerHTML = `<option value="">请选择省份</option> ` + str


}


//封装市
async function getCityData(provinceCode) {

    //市
    const { data: cityData } = await axios.get('/city/city', {
        params: {
            provinceCode
        }
    })


    const cityStr = cityData.data.map((item) =>
        `<option value="${item.city}">${item.name}</option>`

    ).join('')
    qs('#city').innerHTML = `<option value="">请选择城市</option> ` + cityStr


}


//区
async function getAreaData(provinceCode, cityCode) {
    const { data: areaData } = await axios.get('/city/area', {
        params: {
            provinceCode,
            cityCode
        }
    })


    const areaStr = areaData.data.map((item) =>
        `<option value="${item.area}">${item.name}</option>`
    ).join('')
    qs('#county').innerHTML = `<option value="">请选择区县</option> ` + areaStr



}


async function init() {
    await getProvinceData()
    qs('#province').value = 34

    await getCityData('34')
    qs('#city').value = '01'

    await getAreaData('34', '01')

    qs('#county').value = '02'
}
init()


//注册事件
qs('#province').addEventListener('change', async function () {

    await getCityData(this.value)
    qs('#county').innerHTML = `<option value="">请选择区县</option> `

})


qs('#city').addEventListener('change', async function () {
    await getAreaData(qs('#province').value, this.value)


})


